/**
 * Created by huachuan on 2016/1/16.
 */
angular.module("searchfriendcontrollers",[])
  .controller('SearchFriendCtrl',function($scope,$state,$ionicScrollDelegate,$ionicPopup,MeAttend,Storage,ENV,History,Loading,Toast){
    //会员编号
    var memberId = Storage.get("member").member_id;
    $scope.picPath = ENV.picPath;
    //搜索的关键字
    $scope.searchKey = '';
    var haveAttendOrNo = false;
    var foucsFriendIndex = '';
    //关注会员
    $scope.foucsMember = function(toAttendMemberId,index){
      Loading.showLoading();
      foucsFriendIndex = index;
      MeAttend.focusFriend(memberId,toAttendMemberId);
    }
    //取消关注
   // var saveNoAttendMemberId = '';
    $scope.noFoucsMember = function(noAttendMemberId,index){
      $ionicPopup.confirm({
        template: '<p style="text-align: center;font-size:16px">确定要取消关注吗？</p>',
        cancelText: '继续关注',
        okText: '取消关注',
        okType: 'hd-red-btn',
        cancelType: 'cancel-esc-btn'
      }).then(function (res) {
        if (res) {
          //取消对该会员的关注
          foucsFriendIndex = index;
          Loading.showLoading();
          MeAttend.focusFriend(memberId,noAttendMemberId);
        }
      });
    }
    //关注或者取消关注时访问网络成功返回的信息
    $scope.$on('MeAttend.focusFriendUpdated',function(){
      Loading.hideLoading();
      var result = MeAttend.getFocusFriendMsgJson();
      haveAttendOrNo = true;
      if(result.isAttendMsg!='网络异常，请稍后再试'){
       $scope.searchMemberList.memberList[foucsFriendIndex].isAttend = result.isAttend;
       // console.log(result);
      }
      Toast.showMessage(result.isAttendMsg);
    });
    $scope.search = function(){
      if($scope.searchKey == ''){
        Toast.showMessage("请输入搜索的关键词");
      }else{
        Loading.showLoading();
        MeAttend.findMemberListByKey($scope.searchKey,memberId);
      }
      $ionicScrollDelegate.scrollTop();
    }
    $scope.$on('MeAttend.memberListByKeyUpdated',function(){
      Loading.hideLoading();
      $scope.searchMemberList = MeAttend.getMemberListJson();
    });
    //点击用户头像是的操作
    $scope.go2OtherHome  = function(otherMemberId){
      if(memberId==otherMemberId){
        $state.transitionTo("tab.meIndex");
      }else{
        $state.transitionTo("user-dimple-care-list",{memberId:otherMemberId});
      }
    }
    //返回
    $scope.goback = function(){
      History.goback();
      if(haveAttendOrNo){
        MeAttend.getMeAttentionList(memberId);
        haveAttendOrNo = false;
      }
    }
  })

